﻿
//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================


using GuidanceExplorer.Controls;

namespace GuidanceExplorer.Model
{
    class MenuItemUnderSolutionNodeCondition : IMenuItemCondition
    {
        #region IMenuItemCondition Members

    public virtual bool ShowMenuItem(MenuItemAttribute attribute, IModelItem modelItem)
    {
            if (modelItem == null)
            {
                return true;
            }
            else
            {
                FolderStructure container;
                Folder searchFolder;
                    
                if (modelItem is FolderNode)
                {
                    container = ((FolderNode)modelItem).ModelInstance.Container;
                    searchFolder= ((FolderNode)modelItem).ModelInstance.ParentFolder;
                }
                else if(modelItem is ViewNode)
                {
					container = ((ViewNode)modelItem).ModelInstance.Container;
                    searchFolder= ((ViewNode)modelItem).ModelInstance.ParentFolder;
                }
                else if (modelItem is ChecklistNode)
                {
                    container = ((ChecklistNode)modelItem).ModelInstance.Container;
                    searchFolder= ((ChecklistNode)modelItem).ModelInstance.ParentFolder;
                }
                else if (modelItem is GuidanceItemNode || modelItem is GuidanceItemFileReference)
                {
                    ViewNode vNode = ((GuidanceItemNode)modelItem).Parent as ViewNode;
                    ChecklistNode checklistNode = ((GuidanceItemNode)modelItem).Parent as ChecklistNode;
                    SearchViewNode searchViewNode = ((GuidanceItemNode)modelItem).Parent as SearchViewNode;

                    if (vNode != null)
                    {
                        // If Parent of GuidanceItem is a view and CopyItemsByValue is true
                        // then the GuidanceItem is under solution node else no.
                        return (vNode.ModelInstance.CopyItemsByValue);
                    }
                    else if (checklistNode != null)
                    {
                        container = checklistNode.ModelInstance.Container;
                        searchFolder = checklistNode.ModelInstance.ParentFolder;
                    }
                    else if (searchViewNode != null)
                    {
                        container = searchViewNode.ModelInstance.Container;
                        searchFolder = searchViewNode.ModelInstance.ParentFolder;
                    }
                    else
                    {
                        // In this case guidanceItem.Library is not null then it is under library
                        // and not under solution node.
                        return false;
                    }
                }
                else
                {
                    //Esos son los únicos tipos que pueden estar bajo el nodo de la solución.
                    return false;
                }
                if (container != null)
                {
                    if (container.GetType() == typeof(SolutionViews)) 
                        return true;
                    else 
                        return false;
                }
                else
                {
                    while (searchFolder != null)
                    {
                        if ((searchFolder.Container != null) && (searchFolder.Container.GetType() == typeof(SolutionViews))) {
                            return true;
                        }
                        searchFolder = searchFolder.ParentFolder;
                    } 
                   
                    return false;
                }
            }
        }

        #endregion
    }
}

